import { useEffect, useMemo } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { Toast } from '@douyinfe/semi-ui';
import * as tokenUtils from '@/utils/token';

export function useLoginCheck() {
  const routeNavigate = useNavigate();
  const routeLocation = useLocation();

  const currentPath = useMemo(() => routeLocation.pathname, [routeLocation]);

  useEffect(() => {
    if (currentPath.startsWith('/common')) {} else {
      const token = tokenUtils.getToken();
      if (!token) {
        Toast.warning('暂无登录信息，请重新登录');
        routeNavigate('/common/login');
      }
    }
  }, [currentPath]);
}
